//
//  NSDate+Extend.m
//  CoreCategory
//
//  Created by xl on 15/7/16.
//  Copyright (c) 2015年 xl All rights reserved.
//

#import "NSDate+Extend.h"

@interface NSDate ()


/*
 *  清空时分秒，保留年月日
 */
@property (nonatomic,strong,readonly) NSDate *ymdDate;


@end




@implementation NSDate (Extend)




/*
 *  时间戳
 */
-(NSString *)timestamp{

    NSTimeInterval timeInterval = [self timeIntervalSince1970];
    
    NSString *timeString = [NSString stringWithFormat:@"%.0f",timeInterval];
    
    return [timeString copy];
}





/*
 *  时间成分
 */
-(NSDateComponents *)components{
    
    //创建日历
    NSCalendar *calendar=[NSCalendar currentCalendar];
    
    //定义成分
    NSCalendarUnit unit=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    return [calendar components:unit fromDate:self];
}





/*
 *  是否是今年
 */
-(BOOL)isThisYear{
    
    //取出给定时间的components
    NSDateComponents *dateComponents=self.components;
    
    //取出当前时间的components
    NSDateComponents *nowComponents=[NSDate date].components;
    
    //直接对比年成分是否一致即可
    BOOL res = dateComponents.year==nowComponents.year;
    
    return res;
}





/*
 *  是否是今天
 */
-(BOOL)isToday{

    //差值为0天
    return [self calWithValue:0];
}





/*
 *  是否是昨天
 */
-(BOOL)isYesToday{
    
    //差值为1天
    return [self calWithValue:1];
}


-(BOOL)calWithValue:(NSInteger)value{
    
    //得到给定时间的处理后的时间的components
    NSDateComponents *dateComponents=self.ymdDate.components;
    
    //得到当前时间的处理后的时间的components
    NSDateComponents *nowComponents=[NSDate date].ymdDate.components;
    
    //比较
    BOOL res=dateComponents.year==nowComponents.year && dateComponents.month==nowComponents.month && (dateComponents.day + value)==nowComponents.day;
    
    return res;
}



/*
 *  清空时分秒，保留年月日
 */
-(NSDate *)ymdDate{
    
    //定义fmt
    NSDateFormatter *fmt=[[NSDateFormatter alloc] init];
    
    //设置格式:去除时分秒
    fmt.dateFormat=@"yyyy-MM-dd";
    
    //得到字符串格式的时间
    NSString *dateString=[fmt stringFromDate:self];
    
    //再转为date
    NSDate *date=[fmt dateFromString:dateString];
    
    return date;
}


+(NSString *)currentTime
{
    NSDate *date = [NSDate date];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"YYYY-MM-dd-HH:mm:ss"];
    return [df stringFromDate:date];
}


// 比较结果为YES 当前时间大于比较时间 返回YES
+ (BOOL)isEndTime:(NSString *)earlierDate
{
    
    NSDate *current = [NSDate date];
    
    NSDate *dateed = [NSDate dateFormat:earlierDate];
    NSDate *rusult = [current earlierDate:dateed]; //当前时间与 传进来的的时间比较 返回小的那个
    if (rusult == dateed) {
        return YES;
    }
    return NO;
}

+ (NSString *)dateWithStringFromat:(NSString *)dateString
{
    
    NSString *string = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@"-"];
    NSString *string2 =  [string stringByReplacingOccurrencesOfString:@"+08:00" withString:@""];
    
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    // 定制转换格式
    [df setDateFormat:@"YYYY-MM-dd-HH:mm:ss"];

    NSDate *date = [NSDate dateFormat:string2];
    return [NSDate compareCurrentTime:date] ;
}


//  字符串替换的作用是,根据数据接口中存放的时间替判断需不需要转换成系统能转换的时间.
+ (NSDate *)dateFormat:(NSString *)dateString
{
    NSString *string = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@"-"];
    NSString *string2 =  [string stringByReplacingOccurrencesOfString:@"+08:00" withString:@""];
    
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    // 定制转换格式
    [df setDateFormat:@"YYYY-MM-dd-HH:mm:ss"];
    
    return [df dateFromString:string2];

}



// 两个时间比较结果 需要查看定制转换格式 是否能正确转换.
+ (NSComparisonResult )dateWithCompar:(NSString *)date1 date2:(NSString *)date2
{
        NSDate * date = [NSDate dateFormat:date1];
        NSDate * date3 = [NSDate dateFormat:date2];
     return  [date compare:date3];
}

/**
 *  两个时间比较
 *
 *  @param unit     成分单元
 *  @param fromDate 起点时间
 *  @param toDate   终点时间
 *
 *  @return 时间成分对象
 */
+(NSDateComponents *)dateComponents:(NSCalendarUnit)unit fromDate:(NSDate *)fromDate toDate:(NSDate *)toDate{
    
    //创建日历
    NSCalendar *calendar=[NSCalendar currentCalendar];
    
    //直接计算
    NSDateComponents *components = [calendar components:unit fromDate:fromDate toDate:toDate options:0];
    
    return components;
}



/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间
 * @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
+(NSString *) compareCurrentTime:(NSDate*) compareDate
//
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString
                  
                  stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}









@end
